from selenium.webdriver.common.by import By
from pages.base_page import Page


class KBIntegrationPage(Page):
    ele_title = (By.XPATH, u"//h3[text()='KB Integration']")
    ele_error = (By.XPATH, u'//div[@role="alert"]')

    ele_kb_box =(By.XPATH, u'//*[@id="form-integrationKbConfig"]/div/div/div[1]/div[2]/div/div/div/div/div[2]/div/div/div')
    ele_kb_select =(By.XPATH, u'//div[@data-formfield="selectedKb"]')

    ele_kb_search_option = (By.XPATH, u"//p[text()='Knowledge Base Search Options']")
    ele_allow_search_before_chatting = (By.XPATH, u"//div[text()='Allow visitors to search Knowledge Base before chatting']")
    ele_allow_search_before_offline_message = (By.XPATH, u"//div[text()='Allow visitors to search Knowledge Base before submitting an offline message']")
    ele_greeting_message = (By.ID, u'form-integrationKbConfig-textarea-greetingMessageBeforeChatting')
    ele_greeting_message2 = (By.ID, u'form-integrationKbConfig-textarea-greetingMessageBeforeOfflineMessage')
    ele_is_enable_feature_switch = (By.XPATH, u"//input[@name='isEnabled']")

    ele_total_articles =(By.ID, u'form-integrationKbConfig-numberInput-articlesShowedInSearchResult')
    ele_kb_save_button = (By.XPATH, u"(//button[@data-tag='formButton'])[1]")

    # 如果没有KBIntegration功能，则检查图片和unlock button
    ele_img = (By.XPATH, u'//img[@alt="Knowledge Base Introduction"]')
    ele_unlock_this_feature = (By.XPATH, u'//span[text()="Unlock This Feature"]')


    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    # this method should be called first
    def is_enable_feature_switch(self):
        fs = self.find_element(*self.ele_is_enable_feature_switch)
        if fs.get_attribute('value') == "false":
            self.click(self.ele_is_enable_feature_switch)
        else:
            pass

    def check_kb_integration_page_when_has_kb_integration_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_is_enable_feature_switch).is_enabled()
        self.find_element(*self.ele_kb_box).is_enabled()
        self.find_element(*self.ele_allow_search_before_chatting).is_enabled()
        self.find_element(*self.ele_greeting_message).is_enabled()
        self.find_element(*self.ele_allow_search_before_offline_message).is_enabled()
        self.find_element(*self.ele_greeting_message2).is_enabled()

    def check_save_button_when_has_kb_integration_feature(self):
        # self.click(self.ele_kb_box)
        # self.click(self.ele_kb_select)
        # self.input_text(self.ele_greeting_message, '123')
        # self.input_text(self.ele_greeting_message2, '456')
        self.click(self.ele_kb_save_button)

    def check_kb_integration_page_when_no_kb_integration_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_img).is_enabled()
        self.find_element(*self.ele_unlock_this_feature).is_enabled()
